<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="会员名称" prop="memberName">
        <el-input v-model="queryParams.memberName" placeholder="请输入会员名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="店铺名称" prop="storeName">
        <el-input v-model="queryParams.storeName" placeholder="请输入店铺名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="店铺状态" prop="storeStatus" style="width: 200px;">
        <el-select v-model="queryParams.storeStatus" placeholder="请选择">
          <el-option label="开启中" value="0" />
          <el-option label="已关闭" value="1" />
          <el-option label="审核中" value="2" />
          <el-option label="审核拒绝" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createdAt">
        <el-date-picker clearable v-model="queryParams.createdAt" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="danger" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['dianpu:shopinfo:add']">添加</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['dianpu:shopinfo:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['dianpu:shopinfo:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['dianpu:shopinfo:export']">导出</el-button>
      </el-col> -->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shopinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="店铺名称" align="center" prop="storeName" />
      <el-table-column label="会员名称" align="center" prop="memberName" />
      <el-table-column label="店铺地址" align="center" prop="storeLocation" />
      <el-table-column label="是否自营" align="center" prop="isSelfOperated">
        <template #default="scope">
          <el-tag v-if="scope.row.isSelfOperated === 0" type="danger">自营</el-tag>
          <el-tag v-else-if="scope.row.isSelfOperated === 1" type="success">非自营</el-tag>
          <span v-else>{{ scope.row.isSelfOperated }}</span>
        </template>
      </el-table-column>
      <el-table-column label="店铺状态" align="center" prop="storeStatus">
        <template #default="scope">
          <el-tag v-if="scope.row.storeStatus === 0" type="success">开启中</el-tag>
          <el-tag v-else-if="scope.row.storeStatus === 1" type="info">已关闭</el-tag>
          <el-tag v-else-if="scope.row.storeStatus === 2" type="warning">审核中</el-tag>
          <el-tag v-else-if="scope.row.storeStatus === 3" type="danger">审核拒绝</el-tag>
          <span v-else>{{ scope.row.storeStatus }}</span>
        </template>
      </el-table-column>

      <el-table-column label="创建时间，自动设置为当前时间" align="center" prop="createdAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="danger" icon="View" @click="handleView(scope.row)"
            v-hasPermi="['dianpu:shopinfo:view']">查看</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['dianpu:shopinfo:edit']">修改</el-button>
          <el-button v-if="scope.row.storeStatus === 0" link type="warning" icon="Close" @click="handleClose(scope.row)"
            v-hasPermi="['dianpu:shopinfo:close']">关闭</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />


  </div>
</template>

<script setup name="Shopinfo">
import { listShopinfo, getShopinfo, updateShopinfo } from "@/api/dianpu/shopinfo"

const { proxy } = getCurrentInstance()

const shopinfoList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    memberName: null,
    storeName: null,
    createdAt: null,
    storeStatus: null
  }
})

const { queryParams } = toRefs(data)

/** 查询店铺列表列表 */
function getList() {
  loading.value = true
  listShopinfo(queryParams.value).then(response => {
    shopinfoList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  // 跳转到修改页面（用于添加新店铺）
  proxy.$router.push({
    path: '/dianpu/shopinfo/detail1',
    query: { mode: 'add' }
  })
}

/** 查看按钮操作 */
function handleView(row) {
  // 跳转到详情页面，传递店铺ID
  proxy.$router.push({
    path: '/dianpu/shopinfo/detail',
    query: { id: row.id }
  })
}

/** 修改按钮操作 */
function handleUpdate(row) {
  // 跳转到修改页面，传递店铺ID
  proxy.$router.push({
    path: '/dianpu/shopinfo/detail1',
    query: { id: row.id }
  })
}

/** 关闭按钮操作 */
function handleClose(row) {
  // 检查店铺状态，只有开启中的店铺才能关闭
  if (row.storeStatus !== 0) {
    proxy.$modal.msgError("只有开启中的店铺才能关闭")
    return
  }

  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认关闭店铺"' + (row.storeName || _ids) + '"？').then(function () {
    // 获取店铺信息并更新状态为已关闭
    return getShopinfo(_ids).then(response => {
      const shopData = response.data
      shopData.storeStatus = 1 // 设置为已关闭状态
      return updateShopinfo(shopData)
    })
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("关闭成功")
  }).catch(() => { })
}

/** 导出按钮操作 */
// function handleExport() {
//   proxy.download('dianpu/shopinfo/export', {
//     ...queryParams.value
//   }, `shopinfo_${new Date().getTime()}.xlsx`)
// }

getList()
</script>
